package com.ruoyi.reservationserver.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.reservationserver.dao.DynamicDao;
import com.ruoyi.reservationserver.entity.Dynamic;
import com.ruoyi.reservationserver.service.DynamicService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 动态新闻表(Dynamic)表服务实现类
 *
 * @author makejava
 * @since 2023-07-04 16:21:48
 */
@Service("dynamicService")
public class DynamicServiceImpl extends ServiceImpl<DynamicDao,Dynamic> implements DynamicService {
    @Resource
    private DynamicDao dynamicDao;

    /**
     * 通过ID查询单条数据
     *
     * @param dynamicId 主键
     * @return 实例对象
     */
    @Override
    public Dynamic queryById(Integer dynamicId) {
        return this.dynamicDao.queryById(dynamicId);
    }

    /**
     * 分页查询
     *
     * @param dynamic 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<Dynamic> queryByPage(Dynamic dynamic, PageRequest pageRequest) {
        long total = this.dynamicDao.count(dynamic);
        return new PageImpl<>(this.dynamicDao.queryAllByLimit(dynamic, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param dynamic 实例对象
     * @return 实例对象
     */
    @Override
    public Dynamic insert(Dynamic dynamic) {
        this.dynamicDao.insert(dynamic);
        return dynamic;
    }

    /**
     * 修改数据
     *
     * @param dynamic 实例对象
     * @return 实例对象
     */
    @Override
    public Dynamic update(Dynamic dynamic) {
        this.dynamicDao.update(dynamic);
        return this.queryById(dynamic.getDynamicId());
    }

    /**
     * 通过主键删除数据
     *
     * @param dynamicId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer dynamicId) {
        return this.dynamicDao.deleteById(dynamicId) > 0;
    }
}
